#import "SlideshowViewController.h"
#import "SlideshowViewControllerDelegate.h"
#import "SlideshowView.h"

@implementation SlideshowViewController

- (id)initWithDelegate:(id<SlideshowViewControllerDelegate>)_delegate
{
    if (self = [super init])
    {
        delegate = [_delegate retain];
        
        self.wantsFullScreenLayout = YES;
        UIBarButtonItem *leftSpace = [[UIBarButtonItem alloc]
                                      initWithBarButtonSystemItem:
                                      UIBarButtonSystemItemFlexibleSpace
                                      target:nil action:nil];
        leftArrow = [[UIBarButtonItem alloc]
                     initWithImage:[UIImage imageNamed:@"arrowLeft.png"]
                     style:UIBarButtonItemStylePlain
                     target:self action:@selector(showPrevPhoto)];
        UIBarButtonItem *centerSpace = [[UIBarButtonItem alloc]
                                        initWithBarButtonSystemItem:
                                        UIBarButtonSystemItemFlexibleSpace
                                        target:nil action:nil];
        rightArrow = [[UIBarButtonItem alloc]
                      initWithImage:[UIImage imageNamed:@"arrowRight.png"]
                      style:UIBarButtonItemStylePlain
                      target:self action:@selector(showNextPhoto)];
        UIBarButtonItem *rightSpace = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:
                                       UIBarButtonSystemItemFlexibleSpace
                                       target:nil action:nil];
        self.toolbarItems = [NSArray arrayWithObjects:leftSpace, leftArrow,
                             centerSpace, rightArrow, rightSpace, nil];
        [leftSpace release];
        [centerSpace release];
        [rightSpace release];
    }
    return self;
}

- (void)loadView
{
    slideshowView = [[SlideshowView alloc]
                     initWithFrame:CGRectMake(0, 0, 320, 480)
                     controller:self];
    self.view = slideshowView;
    [self reloadData];
    [self showPhotoAtIndex:selectedPhotoIndex];
}

- (void)viewDidUnload
{
    [slideshowView release];
    slideshowView = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    isVisible = YES;
    if (dataReloadPending)
        [self reloadData];
    [self.navigationController setToolbarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    isVisible = NO;
    [self.navigationController setToolbarHidden:YES animated:YES];
}

- (void)dealloc
{
    [leftArrow release];
    [rightArrow release];
    [slideshowView release];
    [delegate release];
    [super dealloc];
}

- (void)reloadData
{
    if (isVisible == NO)
    {
        dataReloadPending = YES;
        return;
    }
    dataReloadPending = NO;
    int n = [delegate slideshowViewControllerPhotosCount:self];
    if (selectedPhotoIndex >= n)
        selectedPhotoIndex = 0;
    [self didNavigate];
    [slideshowView setNumberOfPhotos:n];
    for (int i = 0; i < n; ++i)
        [delegate slideshowViewController:self fetchPhotoAtIndex:i];
}

- (void)fetchedPhoto:(UIImage *)photo atIndex:(int)index
{
    [slideshowView setPhoto:photo atIndex:index];
}

- (void)showPhotoAtIndex:(int)index
{
    selectedPhotoIndex = index;
    [slideshowView showPhotoAtIndex:index];
}

- (void)didNavigate
{
    int n = [delegate slideshowViewControllerPhotosCount:self];
    leftArrow.enabled = (selectedPhotoIndex != 0);
    rightArrow.enabled = (selectedPhotoIndex + 1 != n);
    self.title = [NSString stringWithFormat:@"%d %@ %d", selectedPhotoIndex + 1,
                  NSLocalizedString(@"of", nil), n];
}

- (void)showPrevPhoto
{
    if (selectedPhotoIndex == 0)
        return; // should not happen
    --selectedPhotoIndex;
    [self didNavigate];
    [slideshowView showPhotoAtIndex:selectedPhotoIndex];
}

- (void)showNextPhoto
{
    if (selectedPhotoIndex
        == [delegate slideshowViewControllerPhotosCount:self])
        return; // should not happen
    ++selectedPhotoIndex;
    [self didNavigate];
    [slideshowView showPhotoAtIndex:selectedPhotoIndex];
}

- (void)scrolledToIndex:(int)index
{
    selectedPhotoIndex = index;
    [self didNavigate];
}

@end
